package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.*;
import com.example.demo.mapper.*;
import com.example.demo.service.PersonService;
import com.example.demo.utils.Result;
import com.google.gson.JsonObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import java.text.ParseException;
import java.util.*;




/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cj
 * @since 2023-04-19
 */
@RestController
@Api(tags = "用户管理")
@CrossOrigin
@RequestMapping("/person")
public class PersonController {
    @Autowired
    private PersonService personService;

    @ApiOperation(value = "新增用户")
    @PostMapping("/add")
    public Result add(@RequestBody Person person)throws Exception
    {
        return personService.add(person);
    }
    @PostMapping("/translate")
    @ApiOperation("请求翻译")
    public Result translate(@RequestBody String params) throws Exception
    {
        return personService.translate(params);
    }
    @ApiOperation(value = "根据id删除用户")
    @PostMapping("/delete")
    public Result delete(@RequestParam String id)throws Exception
    {
        LambdaQueryWrapper<Person>personLambdaQueryWrapper=new LambdaQueryWrapper<>();
        personLambdaQueryWrapper.eq(Person::getId,id);
        Person person=personService.getOne(personLambdaQueryWrapper);
        System.out.println(person);
//        Result result=personService.deleteFromOauth(person.getUserName());
        Result result=new Result<>();
        System.out.println(result);
        boolean flag = personService.removeById(id);
        if (flag)
        {
            return Result.OK("删除成功");
        }
        else
        {
            return Result.OK("删除失败");
        }
    }
    @ApiOperation(value = "修改用户信息")
    @PostMapping("/update")
    public Result update(@RequestBody String params) throws Exception {
        Result result=new Result<>();
//        if(personService.checkToken(token).getResult()==null)
//        {
//            result.setMessage("token验证失败");
//            return result;
//        }
//        else
        return personService.update(params);
    }

    @ApiOperation(value = "查询所有用户信息")
    @PostMapping("/get")
    public Result get()
    {
        Result result = new Result();
        List<Person> Persons = personService.list();
        result.setResult(Persons);
        return result;
    }
    @PostMapping("/getByUserName")
    @ApiOperation("根据用户名查询用户信息")
    public Result getByUserName(@RequestParam String userName)
    {
        Result result=new Result<>();
        QueryWrapper<Person>personQueryWrapper=new QueryWrapper<>();
        personQueryWrapper.eq("user_name",userName);
        Person person=personService.getOne(personQueryWrapper);
        String errmsg="";
        if(person==null) {
            result.setResult(null);
            result.setMessage("当前给的这个用户名没查到人");
            return result;
        } else {
            result.setResult(person);
            return result;
        }
    }
    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public Result login(@RequestBody Person person)throws Exception{
        return personService.login(person);
    }
    @ApiOperation(value = "从长江文化大模型问答获取数据")
    @PostMapping("getFromChangJiang")
    public  Result getFromChangJiang(@RequestParam String params) throws Exception {
        Result result=new Result<>();

        if(!params.equals(""))
        {
            return  personService.getFromChangJiang(params);
        }
        else {
            return null;
        }
    }

}

